﻿using System;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using SalesmanCommissionReport.DataSets;

namespace SalesmanCommissionReport.Controls
{
    public partial class FilterXtraForm : XtraForm
    {
        public VendedorDataSet SalesmanData
        {
            set
            {
                vendedorDataSet = value;
                salesmanLookUpEdit.Properties.Columns.Clear();
                salesmanLookUpEdit.Properties.Columns.AddRange(new[] {
                    new LookUpColumnInfo("Id", "Id", 81, FormatType.None, "", false, HorzAlignment.Near),
                    new LookUpColumnInfo("Nome", "Nome", 85, FormatType.None, "", true, HorzAlignment.Near)});
                salesmanLookUpEdit.Properties.DisplayMember = vendedorDataSet.Vendedor.NomeColumn.ColumnName;
                salesmanLookUpEdit.Properties.ValueMember = vendedorDataSet.Vendedor.IdColumn.ColumnName;
                salesmanLookUpEdit.Properties.DataSource = vendedorDataSet.Vendedor;
            }
        }

        public bool Preview
        {
            get { return previewCheckEdit.Checked; }
        }

        public string Salesman
        {
            get { return salesmanLookUpEdit.EditValue == null ? "" : salesmanLookUpEdit.EditValue.ToString(); }
        }

        public DateTime PeriodBegin
        {
            get { return (DateTime)beginDateEdit.EditValue; }
        }

        public DateTime PeriodEnd
        {
            get { return (DateTime)endDateEdit.EditValue; }
        }


        public FilterXtraForm()
        {
            InitializeComponent();
            beginDateEdit.EditValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            endDateEdit.EditValue = beginDateEdit.DateTime.AddMonths(1);
            printSimpleButton.Click += PrintSimpleButtonClick;
        }


        private void ClearControlErrors()
        {
            dxErrorProvider.SetError(beginDateEdit, "");
            dxErrorProvider.SetError(endDateEdit, "");
            dxErrorProvider.SetError(salesmanLookUpEdit, "");
        }

        private bool IsDataValid()
        {
            if (beginDateEdit.EditValue == null)
                dxErrorProvider.SetError(beginDateEdit, "Por favor, escolha a data de início.");
            if (endDateEdit.EditValue == null)
                dxErrorProvider.SetError(endDateEdit, "Por favor, escolha a data de fim.");
            if (beginDateEdit.EditValue != null && endDateEdit.EditValue != null && (DateTime)beginDateEdit.EditValue > (DateTime)endDateEdit.EditValue)
                dxErrorProvider.SetError(endDateEdit, "A data de início tem que ser anterior à data de fim.");
            return !dxErrorProvider.HasErrors;
        }


        void PrintSimpleButtonClick(object sender, EventArgs e)
        {
            ClearControlErrors();
            if (!IsDataValid())
                DialogResult = DialogResult.None;
        }
    }
}
